package cn.csc.module.pay.dal.mysql.fund;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.pay.controller.admin.fund.vo.PayFundExportReqVO;
import cn.csc.module.pay.controller.admin.fund.vo.PayFundPageReqVO;
import cn.csc.module.pay.dal.dataobject.fund.PayFundDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * 用户资金 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface PayFundMapper extends BaseMapperX<PayFundDO> {

    default PageResult<PayFundDO> selectPage(PayFundPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PayFundDO>()
                .eqIfPresent(PayFundDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(PayFundDO::getBalance, reqVO.getBalance())
                .eqIfPresent(PayFundDO::getFreeze, reqVO.getFreeze())
                .eqIfPresent(PayFundDO::getAmount, reqVO.getAmount())
                .eqIfPresent(PayFundDO::getJzbCode, reqVO.getJzbCode())
                .eqIfPresent(PayFundDO::getJzbStatus, reqVO.getJzbStatus())
                .eqIfPresent(PayFundDO::getVMemberId, reqVO.getVMemberId())
                .eqIfPresent(PayFundDO::getAmountQuota, reqVO.getAmountQuota())
                .eqIfPresent(PayFundDO::getInterestRate, reqVO.getInterestRate())
                .eqIfPresent(PayFundDO::getSetUp, reqVO.getSetUp())
                .eqIfPresent(PayFundDO::getContractNo, reqVO.getContractNo())
                .eqIfPresent(PayFundDO::getReinSuccessFlag, reqVO.getReinSuccessFlag())
                .eqIfPresent(PayFundDO::getIpAddress, reqVO.getIpAddress())
                .eqIfPresent(PayFundDO::getMacAddress, reqVO.getMacAddress())
                .eqIfPresent(PayFundDO::getSignChannel, reqVO.getSignChannel())
                .betweenIfPresent(PayFundDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PayFundDO::getId));
    }

    default List<PayFundDO> selectList(PayFundExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<PayFundDO>()
                .eqIfPresent(PayFundDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(PayFundDO::getBalance, reqVO.getBalance())
                .eqIfPresent(PayFundDO::getFreeze, reqVO.getFreeze())
                .eqIfPresent(PayFundDO::getAmount, reqVO.getAmount())
                .eqIfPresent(PayFundDO::getJzbCode, reqVO.getJzbCode())
                .eqIfPresent(PayFundDO::getJzbStatus, reqVO.getJzbStatus())
                .eqIfPresent(PayFundDO::getVMemberId, reqVO.getVMemberId())
                .eqIfPresent(PayFundDO::getAmountQuota, reqVO.getAmountQuota())
                .eqIfPresent(PayFundDO::getInterestRate, reqVO.getInterestRate())
                .eqIfPresent(PayFundDO::getSetUp, reqVO.getSetUp())
                .eqIfPresent(PayFundDO::getContractNo, reqVO.getContractNo())
                .eqIfPresent(PayFundDO::getReinSuccessFlag, reqVO.getReinSuccessFlag())
                .eqIfPresent(PayFundDO::getIpAddress, reqVO.getIpAddress())
                .eqIfPresent(PayFundDO::getMacAddress, reqVO.getMacAddress())
                .eqIfPresent(PayFundDO::getSignChannel, reqVO.getSignChannel())
                .betweenIfPresent(PayFundDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PayFundDO::getId));
    }

    default PayFundDO selectDetail(PayFundPageReqVO reqVO){
        return selectOne(new LambdaQueryWrapperX<PayFundDO>()
                .eqIfPresent(PayFundDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(PayFundDO::getAppUserId, reqVO.getAppUserId()));
    }

    default PayFundDO selectFund(BigInteger merchantId, String appUserId){
        return selectOne(new LambdaQueryWrapperX<PayFundDO>()
                .eqIfPresent(PayFundDO::getMerchantId, merchantId)
                .eqIfPresent(PayFundDO::getAppUserId, appUserId));
    }

    default PayFundDO selectFund(String jzbCode){
        return selectOne(new LambdaQueryWrapperX<PayFundDO>()
                .eqIfPresent(PayFundDO::getJzbCode, jzbCode)
                );
    }

    void addAmount(PayFundDO reqVO);

    void divideAmount(PayFundDO reqVO);

    void drawCashAmount(PayFundDO reqVO);

    void drawCashAddAmount(PayFundDO reqVO);

    /**
     * 冻结撤销
     * @param appUserId 第三方用户ID
     * @param amount 订单金额
     * @param freezeAmt 平安收到的冻结金额（扣除了通联等三方支付的渠道费用）
     * @return
     */
    Integer rechargeCancel(@Param("appUserId")String appUserId, @Param("amount")BigDecimal amount, @Param("freezeAmt")BigDecimal freezeAmt);

}
